
import re

import re

# 你可以使用Python的字符串操作和正则表达式来将一个字符串转换为Windows路径。以下是一个示例代码，演示了如何将字符串转换为Windows路径格式：

# python
import re


from urllib.parse import urlencode

def url_encode(params = {'wd': 'python', 'ie': 'utf-8'}):
    
    # params = {'wd': 'python', 'ie': 'utf-8'}
    result = urlencode(params) # wd=python&ie=utf-8
    return result



def get_url(url,params = {'wd': 'python', 'ie': 'utf-8'}):
    
    # params = {'wd': 'python', 'ie': 'utf-8'}
    result = urlencode(params) # wd=python&ie=utf-8
    url+="?"+result
    return url

def convert_to_valid_win_path(string):
    # 替换特殊符号为下划线
    special_chars = r'[\/:*?"<>|.]' # 需要转义的特殊字符
    string = re.sub(special_chars, '_', string)

    # 替换空格为下划线
    string = string.replace(' ', '_')

    return string

# # 测试字符串
# string = 'This is a sample string / with: special? characters* and spaces.'

# # 转换为有效的Windows路径
# win_path = convert_to_valid_win_path(string)

# # 输出结果
# print(win_path)
# 运行以上代码，将输出This_is_a_sample_string___with__special__characters__and_spaces_，即将字符串转换为了有效的Windows路径格式。

# 在上述代码中，我们使用了re.sub()函数来替换特殊字符为下划线，并使用replace()函数替换空格为下划线。你还可以根据需要扩展special_chars变量，添加其他特殊字符。请注意，转换后的路径仅满足Windows文件名的规范，而不考虑文件系统中文件或目录的实际存在性或合法性。
def find_substring_in(big_str, sub):
    """
    使用in关键字查找子字符串
    """
    if sub in big_str:
        return True
    else:
        return False


def is_chinese_number(text):
    pattern = r'^[一二三四五六七八九十百千万亿零壹贰叁肆伍陆柒捌玖拾佰仟萬億０１２３４５６７８９零壹貳參肆伍陸柒捌玖拾佰仟萬億]*$'
    match = re.match(pattern, text)
    return match is not None
import re

def extract_chinese_numbers(text):
    pattern = r'[一二三四五六七八九十百千万亿零壹贰叁肆伍陆柒捌玖拾佰仟萬億]+'
    matches = re.findall(pattern, text)
    return matches

# def chinese_to_arabic(chinese_num):
#     num_map = {
#         '零': 0,
#         '一': 1,
#         '二': 2,
#         '三': 3,
#         '四': 4,
#         '五': 5,
#         '六': 6,
#         '七': 7,
#         '八': 8,
#         '九': 9,
#         '十': 10,
#         '百': 100,
#         '千': 1000,
#         '万': 10000,
#         '亿': 100000000
#     }

#     result = 0
#     temp = 0
#     for char in chinese_num:
#         if char in num_map:
#             digit = num_map[char]
#             if digit >= 10:
#                 if temp == 0:
#                     temp = digit
#                 else:
#                     temp *= digit
#                     result += temp
#                     temp = 0
#             else:
#                 temp += digit
#         else:
#             return None

#     result += temp
#     return result


def chinese_to_arabic(chinese_num):
    num_map = {
        '零': 0,
        '一': 1,
        '二': 2,
        '三': 3,
        '四': 4,
        '五': 5,
        '六': 6,
        '七': 7,
        '八': 8,
        '九': 9,
        '十': 10,
        '百': 100,
        '千': 1000,
        '万': 10000,
        '亿': 100000000
    }

    result = 0
    temp = 0
    for char in chinese_num:
        if char in num_map:
            digit = num_map[char]
            if digit >= 10:
                if temp == 0:
                    temp = digit
                else:
                    temp *= digit
                    result += temp
                    temp = 0
            else:
                temp += digit
        else:
            return None

    result += temp
    return result

# def chinese_to_arabic(chinese_num):
#     num_map = {
#         '零': 0,
#         '一': 1,
#         '二': 2,
#         '三': 3,
#         '四': 4,
#         '五': 5,
#         '六': 6,
#         '七': 7,
#         '八': 8,
#         '九': 9,
#         '十': 10,
#         '百': 100,
#         '千': 1000,
#         '万': 10000,
#         '亿': 100000000
#     }

#     result = 0
#     temp = 0
#     last_unit = 1  # 用于记录前一个单位
#     for char in chinese_num:
#         if char in num_map:
#             digit = num_map[char]
#             if digit >= 10:
#                 if temp == 0:
#                     temp = digit
#                 else:
#                     temp *= digit
#                     result += temp
#                     temp = 0
#             else:
#                 if last_unit < digit:  # 处理十、百、千前没有数字的情况
#                     temp = last_unit * digit
#                 else:
#                     temp += digit
#                 last_unit = digit
#         else:
#             return None

#     result += temp
#     return result

# 测试
# print(chinese_to_arabic("二十一"))  # 21
# print(chinese_to_arabic("一千零二十五"))  # 1025
# print(chinese_to_arabic("五百六十万"))  # 5600000



def sub_strs_start_end_all(str, start, end):
    # https://zhidao.baidu.com/question/2057789258998428427.html
    things = list()
    # https://www.cnblogs.com/xingchuxin/p/10427391.html
    while True:
        start_pos = str.find(start)
        if start_pos == -1:
            break

        end_pos = str.find(end)
        if end_pos == -1:
            break
        while True:
            # https://blog.csdn.net/weixin_33739523/article/details/93816000
            startAnotherPos = str.find(start, start_pos + 1)
            # 如果有不符合的字符串，比如说https:3,https:4.jar,3后面没有.jar，就要把3的这个去掉
            # 如果https后面还有个https，但是中间没有.jar，说明前面的https是不正确的格式，要舍去
            if startAnotherPos == -1:
                break
            if startAnotherPos < end_pos:
                start_pos = startAnotherPos
            if start_pos > end_pos:
                end_another_pos = str.find(end, end_pos + 1)
                end_pos = end_another_pos
            else:
                break
        sub_str = str[start_pos:end_pos] + end

        things.append(sub_str)

        str = str[end_pos + len(end):]
    things_final = list()
    # 这边不知道为什么会有不是以start开头的也被过滤进来了，想搞清楚太麻烦，还是再过滤一次好了
    for thing in things:
        if thing.startswith(start):
            things_final.append(thing)
    return things_final

def find_chinese(file):

     pattern = re.compile(r'[^\u4e00-\u9fa5]')

     chinese = re.sub(pattern, '', file)

    #  print(chinese)
     return chinese


def to_left_slash(right_slash):
    return right_slash.replace('\\',r'/')
    # print(right_slash.replace('\\',r'/'))
 

def find_english(file):
    pattern = re.compile(r'[\u4e00-\u9fa5]')
    english = re.sub(pattern, '', file)
    print(english)

 
 
def camel_to_underscore(name,):
    '''
    将参数名的驼峰形式转为下划线形式
    @param params:
    @return:
    '''
    new_name = ""
    name += " "  # 为了防止数据溢出
    for i in range(len(name) - 1):
        if i == 0:
            new_name += name[i]
        elif name[i].isupper() and name[i - 1].islower():
            new_name += "_" + name[i]
        # 如果不在前面加上name += " "，这里会索引越界
        elif name[i].isupper() and name[i - 1].isupper() and name[i + 1].islower():
            new_name += "_" + name[i]
        else:
            new_name += name[i]
    return  new_name

def print_map(map):
    for k,v in map.items():
        print(k)
        print(v)
        print('----------------')
        
def camel_to_underscore_lower(name,):
    '''
    将参数名的驼峰形式转为下划线形式
    @param params:
    @return:
    '''
    name=camel_to_underscore(name)
    return  name.lower()

def eng_to_underscore_lower(name,):
    '''
    将参数名的驼峰形式转为下划线形式
    @param params:
    @return:
    '''
    name=camel_to_underscore(name)
    # -
    return  name.lower().replace(" ","_").replace(':','').replace(',','').replace('-','_')




# https://blog.csdn.net/mouday/article/details/81512870
def contains_chinese(check_str):
    """
    判断字符串中是否包含中文
    :param check_str: {str} 需要检测的字符串
    :return: {bool} 包含返回True， 不包含返回False
    """
    for ch in check_str:
        if u'\u4e00' <= ch <= u'\u9fff':
            return True
    return False



def back_del_str(old_str, dont_want):
    if dont_want == "":
        return old_str
    old_str_len = len(old_str)
    dont_want_len = len(dont_want)
    i_old = old_str_len - 1
    i_dont = dont_want_len - 1

    i_res = old_str_len
    i_now = old_str_len
    while 1:
        if not old_str[i_old] == dont_want[i_dont]:
            return old_str[:i_res]
        i_now -= 1
        if i_res - i_now == dont_want_len:
            i_res -= dont_want_len
        if i_dont == 0 or i_old == 0:
            return old_str[:i_res]

        i_old -= 1
        i_dont -= 1


def front_del_str(old_str, dont_want):
    if dont_want == "":
        return old_str
    old_str_len = len(old_str)
    dont_want_len = len(dont_want)
    min_len = min(old_str_len, dont_want_len)
    i_old = 0
    i_dont = 0

    i_res = 0
    i_now = 0
    while 1:
        if not old_str[i_old] == dont_want[i_dont]:
            return old_str[i_res:]
        i_now += 1
        # 1234
        # 12
        if i_now - i_res == dont_want_len:
            i_res += dont_want_len
        if i_dont == min_len - 1 or i_old == min_len - 1:
            return old_str[i_res:]

        i_old += 1
        i_dont += 1


# https://zhuanlan.zhihu.com/p/93671522


def camel(s):
    s = re.sub(r"(_|-)+", " ", s).title().replace(" ", "")
    return s[0].lower() + s[1:]


import re


def to_camel_case(x):
    """转驼峰法命名"""
    return re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)


def to_upper_camel_case(x):
    """转大驼峰法命名"""
    s = re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)
    return s[0].upper() + s[1:]


def to_lower_camel_case(x):
    """转小驼峰法命名"""
    s = re.sub('_([a-zA-Z])', lambda m: (m.group(1).upper()), x)
    return s[0].lower() + s[1:]

def decimal_places(num, places=2):
    return f'%.{places}f' %(num)

# ————————————————
# 版权声明：本文为CSDN博主「nie303671298」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
# 原文链接：https://blog.csdn.net/nie303671298/article/details/108146480


if __name__=="__main__":
    pass

    print(to_camel_case('UserLoginCount'))  # UserLoginCount
    print(to_camel_case('userLoginCount'))  # userLoginCount
    print(to_camel_case('user_login_count'))  # userLoginCount
    print()

    print(to_upper_camel_case('UserLoginCount'))  # UserLoginCount
    print(to_upper_camel_case('userLoginCount'))  # UserLoginCount
    print(to_upper_camel_case('user_login_count'))  # UserLoginCount
    print()

    print(to_lower_camel_case('UserLoginCount'))  # userLoginCount
    print(to_lower_camel_case('userLoginCount'))  # userLoginCount
    print(to_lower_camel_case('user_login_count'))  # userLoginCount


    chinese_article="""
    cherry
    樱桃
    watermelon
    西瓜
    grape
    葡萄
    honey
    peach水蜜桃
    mango芒果
    pomegranate石榴
    plum李子
    pomelo柚子
    olive橄榄
    litchi荔枝
    lemon柠檬
    longan龙眼
    coconut椰子
    sugarcane甘蔗
    haw/hawthorn
    山楂
    chestnut栗子
    persimmon柿子
    guava
    番石榴
    almond杏仁
    等等
    """

    chinese_article="""
    Seeds: 瓜子



    To shell seeds: 剝壳、嗑瓜子



    Dried fish：鱼片干



    Dried cuttlefish: 鱿鱼丝



    Animal parts：内脏



    Duck tongues: 鸭舌头



    Duck wings: 鸭翅膀



    Spicy gluten: 辣条



    Seaweed: 海苔"""
    chinese_lines=chinese_article.split("\n")

    chinese_word_list=[]
    for chinese_line in chinese_lines:
        if chinese_line =="":
            continue
        chinese_word=find_chinese(chinese_line)
        if chinese_word is None:
            continue
        if chinese_word.isspace():
            continue
        if chinese_word =="":
            continue
        # print(chinese_word)
        chinese_word_list.append(chinese_word)

    # print(chinese_word_list)


    # find_chinese("""
    # cherry
    # 樱桃
    # watermelon
    # 西瓜
    # grape
    # 葡萄
    # honey
    # peach水蜜桃
    # mango芒果
    # pomegranate石榴
    # plum李子
    # pomelo柚子
    # olive橄榄
    # litchi荔枝
    # lemon柠檬
    # longan龙眼
    # coconut椰子
    # sugarcane甘蔗
    # haw/hawthorn
    # 山楂
    # chestnut栗子
    # persimmon柿子
    # guava
    # 番石榴
    # almond杏仁
    # 等等
    # """)
    # 测试
    # print(is_chinese_number("一二三"))  # True
    # print(is_chinese_number("123"))  # False
    # print(is_chinese_number("一二三456"))  # False
    # print(is_chinese_number("一二三零"))  # True

    
    # 测试
    # print(chinese_to_arabic("二十一"))  # 21
    # print(chinese_to_arabic("一千零二十五"))  # 1025
    # print(chinese_to_arabic("五百六十万"))  # 5600000
    # 21
    # 1025
    # 10560

        
    # 测试
    text = "这是一个包含一百二十三和五千六百七十八的字符串"
    numbers = extract_chinese_numbers(text)
    # ['一', '一百二十三', '五千六百七十八']
    print(numbers)  # ['一百二十三', '五千六百七十八']



